12. Access Modifers

Access Modifiers

When we are writing our code, we sometimes need to restrict access to certain data in the application in order to ensure it doesn't get modified in an unintended or harmful way.

For example, suppose we define a variable in one part of our application called employeePay. If we allow open access to this variable from any part of the application, it may be possible for someone to accidentally (or maliciously) access the data in this variable to find out how much someone else is paid—or even modify that amount!

Fortunately, we can control which parts of our code can access the data in other parts. We do this using Java's access modifiers.

Access modifiers determine how other classes are allowed to access your variables and methods.

Classes, Subclasses, and Packages

In order to explain access modifiers, we'll be talking about classes, subclasses, and the related idea of packages. We haven't defined or explained any of these terms yet, but we will go over them thoroughly in the next few lessons. However, we need to get at least some idea about what access modifiers are now, because (as you'll see in a moment) we have already started using them!

For the moment, it is enough to think about classes and packages as ways of organizing your code. For example, in the Getting Started exercise we did earlier in this lesson, you may have noticed that all of your code was wrapped up inside a class:

public class GettingStarted {
    // Anything between the curly braces is inside the class!
}

// Anything outside the curly braces is outside the class!

In this case, the keyword public is an example of an access modifier. Access modifiers control things like whether the data inside of a class can be accessed only by other code inside of the class or also by code elsewhere in the application.

Not everything on this page may make perfect sense to you until you have gotten practice creating your own classes (and subclasses!). For now, just try to get the general idea. You may wish to revisit this page later on after you've completed the next two lessons.

Types of Access Modifiers

There are four types of access modifiers in Java:

  • Public means the class can be accessed from everywhere. If you have a method on a class that you want to expose to all other classes, then use this access modifier.
  • Private means only the defining class can access the data. This provides security, by not allowing other classes to change the data directly. Instead, they must make changes to the data via the provided methods only.
  • Protected means that access is restricted to the defining class, package, or subclass. This will be useful when we get into subclasses and inheritance in a later lesson, as it will allow our subclasses to use variables and methods from the parent class.
  • Default means access is restricted to the defining class or the package. This can be used when we have classes inside the same package that we may want to expose data and methods too.

Here's a summary in a table format for your reference:

Access Inside class Inside package Outside package by subclass Outside package
Private Yes No No No
Default Yes Yes No No
Protected Yes Yes Yes No
Public Yes Yes Yes Yes

The protected access modifier can be accessed from the following:

(Select all that apply.)

SOLUTION:
  • From within the defining **class**
  • From inside the **same package** as the defining class
  • From outside the package by a **subclass**

Access Modifiers on Methods

We can also apply access modifiers to methods. Here are some examples, just so you can see what they look like:

private void methodName()
void methodName()  // The default is no access modifier
protected void methodName()
public void methodName()

Access Modifiers on Variables

And we can apply access modifiers to variables as well! We will get into this in detail in the lessons on object-oriented programming. Here are some examples—again, just so you can get the idea:

private int number;
int number3;  // Default is no access modifier
protected int number2;
public int number1;

Suppose we have a class named Calculator and inside that class we define the following method:

private void addNumber(int x, int  y){
    return x + y
}

Now suppose that we try to call this method (e.g., addNumbers(2, 2)) from a different class that is in a different package. Would this work?

SOLUTION: No, the private access modifier cannot be accessed from outside of the defining class.

Which of the following changes would allow the example above to work?

SOLUTION: `public void addNumber(int x, int y)`